package com.designoutput.ui.gallery
{
	import com.designoutput.ui.Hover;
	import com.designoutput.ui.controls.Literal;
	import com.designoutput.ui.newControls.ControlBase;
	import com.designoutput.utils.Globals;
	import com.designoutput.vo.GalleryImageVo;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;

	/**
	 * The image element use to load image for gallery.
	 */
	public class ImageItem extends ControlBase
	{	
		private var _imageVo:GalleryImageVo;
		
		private var _loader:Loader;
		private var _titleTextField:TextField;
		
		public var imageBody:DisplayObject;	
		
		override protected function initialize():void {			
			super.initialize();
			
			this.buttonMode = true;
			
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedHandler);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
			
			_titleTextField = new TextField();
			var textFormat:TextFormat = new TextFormat("Verdana, Helvetica, San-serif", 12, 0);
			textFormat.align = TextFormatAlign.CENTER;			
			_titleTextField.defaultTextFormat = textFormat;
			_titleTextField.mouseEnabled = false;
			addChild(_titleTextField);
			
			this.addEventListener(MouseEvent.MOUSE_OVER, overHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
		}
		
		override protected function updateDisplay():void {
			super.updateDisplay();
			
			if(imageBody != null){
				imageBody.x = ( _width - imageBody.width ) / 2;
				imageBody.y = ( _height - imageBody.height ) / 2;
			}
			
			_titleTextField.width = _width;
			_titleTextField.y = _height - 22;
			
			drawBackground();
		}
		
		public function get imageVo():GalleryImageVo {
			return _imageVo;
		}
		public function set imageVo(vo:GalleryImageVo):void
		{
			_imageVo = vo;
			_loader.load(new URLRequest(Globals.applicationUrl + vo.thumbUrl));
			_titleTextField.text = vo.title;
		}

		public function loadedHandler(evt:Event):void
		{
			imageBody = _loader.content;
			if(imageBody is Bitmap)
				(imageBody as Bitmap).smoothing = true;
			
			this.addChild(imageBody);
			
			invalidateDisplay();
		}

		public function errorHandler(evt:Event):void
		{
			trace("ImageItem::errorHandler");
			this.dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false, ErrorEvent(evt).text));
		}

		private function drawBackground(color:Number=0xd2d0d0, alpha:Number=0.7):void
		{
			this.graphics.clear();
			this.graphics.lineStyle(1, 0xFFFFFF);//0x1d1712
			this.graphics.drawRect(0, 0, _width, _height);
			this.graphics.beginFill(color, alpha);
			this.graphics.drawRect(1, 1, _width - 2, _height - 2);
			this.graphics.endFill();
		}

		private function overHandler(evt:MouseEvent):void
		{
			drawBackground(0, 1);
			
			this.filters = [new DropShadowFilter(3,45,0,0.7)];
			if(this._imageVo.title != ""){
				Globals.designoutputCard.hover.setText(this._imageVo.title);
			}
		}

		private function outHandler(evt:*):void
		{
			drawBackground();
			
			this.filters = [];
			Globals.designoutputCard.hover.hideEvent(evt);
		}

	}
}